import 'package:example/test/page/ExamplePage.dart';
import 'package:example/test/page/IndicatorPage.dart';
import 'package:example/test/page/TestPage.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'refresh_page.dart';

import "package:flutter_localizations/flutter_localizations.dart";

void main() {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  runApp(const MyApp());
  updateStatusBarColor(Colors.transparent, true);
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: const Size(375, 812),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, child) {
          return _defaultRefreshConfiguration(GetMaterialApp(
            title: "超会聊",
            // 不需要国际化，可以删除：localizationsDelegates: [] ,supportedLocales: []
            localizationsDelegates: const [
              RefreshLocalizations.delegate,
              GlobalMaterialLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
            ],
            supportedLocales: const [
              Locale('en'),
              Locale('zh'),
            ],
            // 默认语言
            locale: const Locale('zh'),
            debugShowCheckedModeBanner: false,
            defaultTransition: Transition.rightToLeft,
            transitionDuration: const Duration(milliseconds: 300),
            theme: ThemeData(
                colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
                useMaterial3: true,
                tabBarTheme:
                    const TabBarTheme(dividerColor: Colors.transparent)),
            home: Scaffold(
              appBar: AppBar(title: const Text("Pull-Refresh")),
              body: Column(
                children: [
                  TextButton(
                    style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Colors.blue),
                    ),
                    onPressed: () {
                      Get.to(ExamplePage());
                    },
                    child: const Text(
                      "基础用法，混合用法",
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  const SizedBox(
                    height: 5,
                  ),
                  TextButton(
                    style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Colors.blue),
                    ),
                    onPressed: () {
                      Get.to(IndicatorPage(title: "自定义指示器"));
                    },
                    child: const Text(
                      "自定义指示器",
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  const SizedBox(
                    height: 5,
                  ),
                  TextButton(
                    style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Colors.blue),
                    ),
                    onPressed: () {
                      Get.to(TestPage(title: "Test"));
                    },
                    child: const Text(
                      "不同Tab界面下的使用",
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                  const SizedBox(
                    height: 5,
                  ),
                  TextButton(
                    style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Colors.blue),
                    ),
                    onPressed: () {
                      Get.to(RefreshIndex());
                    },
                    child: const Text(
                      "Test 下拉刷新，上拉加载",
                      style: TextStyle(color: Colors.white),
                    ),
                  ),
                ],
              ),
            ),
          ));
        });
  }
}

RefreshConfiguration _defaultRefreshConfiguration(Widget child) {
  return RefreshConfiguration(
      headerBuilder: () => const MaterialClassicHeader(),
      footerBuilder: () => const ClassicFooter(),
      // 头部触发刷新的越界距离
      headerTriggerDistance: 60.w,
      //自定义回弹动画,三个属性值意义请查询flutter api
      springDescription: const SpringDescription(
        mass: 2.2,
        stiffness: 150,
        damping: 16,
      ),
      //头部最大可以拖动的范围,如果发生冲出视图范围区域,请设置这个属性
      maxOverScrollExtent: 20,
      // 底部最大可以拖动的范围
      maxUnderScrollExtent: 0,
      //这个属性不兼容PageView和TabBarView,如果你特别需要TabBarView左右滑动,你需要把它设置为true
      enableScrollWhenRefreshCompleted: true,
      //在加载失败的状态下,用户仍然可以通过手势上拉来触发加载更多
      enableLoadingWhenFailed: false,
      // Viewport不满一屏时,禁用上拉加载更多功能
      hideFooterWhenNotFull: true,
      // 可以通过惯性滑动触发加载更多
      enableBallisticLoad: true,
      footerTriggerDistance: 15,
      dragSpeedRatio: 0.91,
      shouldFooterFollowWhenNotFull: (state) {
        // If you want load more with noMoreData state ,may be you should return false
        return false;
      },
      child: child);
}

void updateStatusBarColor(Color color, bool isDark) {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: color,
      statusBarIconBrightness: isDark ? Brightness.dark : Brightness.light));
}
